// pages/store/detail/index.js
import {
  getStoreByIdApi,
  postStoreCartApi,
  getStoreCartApi,
  postStoreOrderApi
} from '../../../https/store'
import {
  BASE_URL
} from '../../../constant/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    bannerList: [],
    current: 0,
    data: {
      covers: ["", "", "", ""],
      title: "",
      price: 0,
      desc: ''
    },
    total: 0,

    addressList: [],
    address: {},
    notes: '',
    showAddress: false,
    showOrder: false
  },
  onLoad(option) {
    const docid = option.docid || '124411069009'
    this.getData(docid)
    this.getStoreCart()

    this.getAddress()
    this.setData({
      address: this.data.addressList[0]
    })
  },
  onShow() {
    console.log(13)
    this.getAddress()
  },
  // 获取数据
  async getData(docid) {
    const getRes = await getStoreByIdApi({
      id: docid
    })
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        data: getRes.data
      })
    }
  },
  // 点击切换轮播图
  bindSwiper(e) {
    const current = e.currentTarget.dataset.index
    this.setData({
      current
    })
  },
  // 加入购物车
  async addCart() {
    const {
      id
    } = this.data.data
    wx.showLoading({
      title: '加入中..',
    })
    const postCart = await postStoreCartApi({
      storeId: id
    })
    console.log(postCart)
    if (postCart.code == 200 || postCart.code == 201) {
      wx.showToast({
        title: '已加入购物车',
        icon: 'success'
      })
      this.getStoreCart()
    }
    if (postCart.statusCode == 403) {
      wx.navigateTo({
        url: '../../my/login/index',
      })
      return
    }


  },
  // 轮播图切换
  swiperChange: function (e) {
    this.setData({
      current: e.detail.current
    });
  },
  // 获取购物袋数量
  async getStoreCart() {
    const getRes = await getStoreCartApi()
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        total: getRes.data.total
      })
    }
  },
  // 立即购买
  async nowBuy() {
    console.log(this.data.data)
    this.setData({
      showOrder: true
    })
  },
  // 获取收货地址
  getAddress() {
    const addressList = wx.getStorageSync('cardAddress') || []
    console.log(addressList)
    this.setData({
      addressList
    })
  },
  // 显示收货地址
  showAddAddress() {
    this.setData({
      showOrder: false,
      showAddress: true
    })
  },
  // 切换收货地址
  toggleAddress(e) {
    console.log(e)
    const docid = e.currentTarget.dataset.docid
    console.log(this.data.addressList)
    const addressList = this.data.addressList
    const newAddress = addressList.map((item) => {
      if (item.id == docid) {
        item.select = true
      } else {
        item.select = false
      }
      return item
    })
    console.log(newAddress)
    wx.setStorageSync('cardAddress', newAddress)

    this.getAddress()
  },
  // 删除收货地址
  async deleteAddress(e) {
    const {
      cancel
    } = await wx.showModal({
      title: '提示',
      content: '是否删除该收货地址',

    })
    if (cancel) return

    const docid = e.currentTarget.dataset.docid
    const addressList = this.data.addressList
    const newAddress = addressList.filter((item) => {

      return item.id != docid
    })
    console.log(newAddress)
    wx.setStorageSync('cardAddress', newAddress)
    this.getAddress()
  },
  // 选择收货地址
  selectAdderess(e) {
    console.log(this.data.addressList)
    const address = this.data.addressList.filter(item => item.select)
    console.log(address)
    if (!address[0]) {
      wx.showToast({
        title: '请添加收货地址',
        icon: 'none'
      })
      return
    }
    this.setData({
      address: address[0],
      showAddress: false,
      showOrder: true
    })
  },
  // 关闭购买弹框
  onClose() {
    this.setData({
      showOrder: false,
      showAddress: false
    })
  },
  // 创建订单
  async createOrder() {
    console.log('创建订单')

    const {
      notes,
      address,
      data
    } = this.data
    console.log(this.data)
    if (!address) {
      wx.showToast({
        title: '请填写配送地址',
        icon: 'none'
      })
      return
    }
    const cartIds = []
    const storeId = data.id
    delete address.select
    delete address.id
    console.log(data.id)
    wx.showLoading({
      title: '创建订单中..',
      mask: true
    })
    const postRes = await postStoreOrderApi({
      cartIds,
      storeId,
      address,
      notes
    })
    console.log(postRes)
    if (postRes.statusCode == 403) {
      wx.hideLoading()
      wx.navigateTo({
        url: '../../my/login/index',
      })
      return
    }

    if (postRes.code == 200) {
      setTimeout(() => {
        wx.hideLoading()
        wx.navigateTo({
          url: '../order/index?docid=' + postRes.data.id,
        })
        // 关闭弹框
        this.onClose()
      }, 1200)
    } else {
      wx.showToast({
        title: '创建订单失败',
        icon: 'error'
      })
    }
    wx.hideLoading()
  },
  // 跳转购物袋
  gotoShoppingBag() {
    wx.navigateTo({
      url: '../shoppingBag/index',
    })
  },
  // 跳转添加购物地址页面
  gotoAddAddress() {
    wx.navigateTo({
      url: '../addAddress/index',
    })
  }


})